/*
  in_unreal.dll - Winamp Plugin 
  
  Andrzej Oczkowicz [andREV] 
  andrew_lz@poczta.fm
  
  Config Box dialog code  
  
*/


#include <windows.h>
#include <commdlg.h>
#include <stdio.h>
#include <shlobj.h>
#include "dll.h"
#include "in.h"

HWND howner, self;
HINSTANCE inst;

extern config_s cfg_vals;
extern In_Module *it;

/* Config functions */
const buff_size = 1024 * 4;
const char *sTRUE = "True";
const char *sFALSE = "False";
const char *dSECTION = "In Unreal Music Decoder";



char *getConfigStr(char *section, char *key, char *def, char *buffer)
{
    GetPrivateProfileString(section, key, def, buffer, buff_size, cfg_vals.file);
    return buffer;   
}     

void setConfigStr(char *section, char *key, char *val)
{
    WritePrivateProfileString(section, key, val, cfg_vals.file);   
} 

BOOL getConfigBool(char *section, char *key, BOOL def)
{
  char cfgb[buff_size];
  char *bstr;
  if (def)
    bstr = (char *)sTRUE;
  else
    bstr = (char *)sFALSE;
  bstr = (char *)getConfigStr(section, key, bstr, cfgb);
  if (!strcasecmp(bstr, sFALSE))
    return FALSE;
  else
    return TRUE;  
}  

void setConfigBool(char *section, char *key, BOOL val)
{
  char *bstr;
  if (val)
    bstr = (char *)sTRUE;
  else
    bstr = (char *)sFALSE;
  setConfigStr(section, key, bstr);  
}

void read_config()
{  
    char cfgbuffer[buff_size];
    //char *cmd = GetCommandLine();
    char cbf[buff_size];
    GetModuleFileName(0, cbf, buff_size);
    char *cmd = cbf;
    cfg_vals.file = (char *)getString(cmd);
    m(cfg_vals.file);  
    int i;
    for (i = strlen(cfg_vals.file); cfg_vals.file[i] != '.'; i--)
      cfg_vals.file[i] = 0;
    strcat(cfg_vals.file, "ini");
    m("configfile:");
    m(cfg_vals.file);     
   
   cfg_vals.in_mod  = (char *)getString( getConfigStr((char *)dSECTION, "in_mod", "in_mod.dll4", cfgbuffer ));
   cfg_vals.tmp_dir = (char *)getString( getConfigStr((char *)dSECTION, "tmp_dir", "C:\\", cfgbuffer ));
   cfg_vals.bDelete = getConfigBool((char *)dSECTION, "bDelete", TRUE) ;
   cfg_vals.bTmp = getConfigBool((char *)dSECTION, "bTmp", TRUE) ;  
}

void save_config(BOOL bDeleteStrings)
{ 
  if (cfg_vals.bConfigChanged){
   setConfigStr((char *)dSECTION, "in_mod", cfg_vals.in_mod);
   setConfigStr((char *)dSECTION, "tmp_dir", cfg_vals.tmp_dir);
   setConfigBool((char *)dSECTION, "bDelete", cfg_vals.bDelete) ;
   setConfigBool((char *)dSECTION, "bTmp", cfg_vals.bTmp) ;
   cfg_vals.bConfigChanged = FALSE;
  }       
  if (bDeleteStrings){
    delString(&cfg_vals.in_mod);
    delString(&cfg_vals.tmp_dir);
    delString(&cfg_vals.file);   
  } 
}


/* eof cfg funcs */
    

HWND ed_mod, ed_ini, cb_usetmp, ed_tmpdir, cb_delete, btn_br;

void text_fromctrl(HWND ctrl, char **buffer)
{
   delString(buffer);
   int len = GetWindowTextLength(ctrl) + 1;
   *buffer = malloc(len);
   GetWindowText(ctrl, *buffer, len);   
  
}  


void set_defaults()
{
  if ( MessageBox(self, "Set default values?", "Are you sure?", MB_OKCANCEL | MB_ICONQUESTION) != IDOK )
    return;
  SetWindowText(ed_mod, "in_mod.dll");
  SetWindowText(ed_tmpdir, (char *)tmpDir());
  EnableWindow(ed_tmpdir, FALSE);
  EnableWindow(btn_br, FALSE);
  SendMessage(cb_usetmp, BM_SETCHECK, BST_CHECKED, 0);
  SendMessage(cb_delete, BM_SETCHECK, BST_CHECKED, 0);
}  

BOOL EnumChildProc(HWND hwnd, LPARAM lParam)
{
  int i = GetWindowLong(hwnd, GWL_ID);
  switch (i)
  {
   case 777: cb_usetmp = hwnd;
     break;
   case 888: ed_tmpdir = hwnd;
     break;      
   case 999: cb_delete = hwnd;
     break;
   case 555: ed_mod = hwnd;
     break;   
   case 100: ed_ini = hwnd;
     break;   
   case 444: btn_br = hwnd;
     break;               
          
  }  
  return TRUE;
}  
   

LRESULT cfg_dlg(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{	 
	switch(msg)
	{	
		case WM_INITDIALOG:{
		   self = hwnd;
		   EnumChildWindows(hwnd, (ENUMWINDOWSPROC)EnumChildProc, 0); 
		   SetWindowText(ed_ini, cfg_vals.file);
		   if (cfg_vals.bTmp){
         SendMessage(cb_usetmp, BM_SETCHECK, BST_CHECKED, 0);
         SetWindowText(ed_tmpdir, (char *)tmpDir());
      }else
      {
         SendMessage(cb_usetmp, BM_SETCHECK, BST_UNCHECKED, 0);
         EnableWindow(ed_tmpdir, TRUE);
         EnableWindow(btn_br, TRUE);
         SetWindowText(ed_tmpdir, cfg_vals.tmp_dir);
      }   
    
      SetWindowText(ed_mod, cfg_vals.in_mod);
      
		  if (cfg_vals.bDelete)
         SendMessage(cb_delete, BM_SETCHECK, BST_CHECKED, 0);
      else
         SendMessage(cb_delete, BM_SETCHECK, BST_UNCHECKED, 0);  
      break;
     }	 		
  	case WM_COMMAND:
  	   switch(wParam)
       {
         case 777:{
            int i = SendMessage(cb_usetmp, BM_GETCHECK, 0, 0);
            cfg_vals.bConfigChanged = TRUE;
            switch (i)
            { 
              case BST_CHECKED: 
                 text_fromctrl(ed_tmpdir, &cfg_vals.tmp_dir);           
                 SetWindowText(ed_tmpdir, (char *)tmpDir() );
                 EnableWindow(ed_tmpdir, FALSE);
                 EnableWindow(btn_br, FALSE);
                break;
              case BST_UNCHECKED: 
                 SetWindowText(ed_tmpdir, cfg_vals.tmp_dir);
                 EnableWindow(ed_tmpdir, TRUE); 
                 EnableWindow(btn_br, TRUE);          
                break;  
                            	
            }            
           break;
         }    
         case 111: // ok
             cfg_vals.bTmp = (SendMessage(cb_usetmp, BM_GETCHECK, 0, 0) == BST_CHECKED);
             cfg_vals.bDelete = (SendMessage(cb_delete, BM_GETCHECK, 0, 0) == BST_CHECKED);
             if (!cfg_vals.bTmp)
               text_fromctrl(ed_tmpdir, &cfg_vals.tmp_dir);
             text_fromctrl(ed_mod, &cfg_vals.in_mod);
             cfg_vals.bConfigChanged = TRUE;
             save_config(FALSE);
             SendMessage(hwnd, WM_DESTROY, 0, 0);
             if (!it)
               copyAndLoad(); 
           break; 
           
         case 222: // mod options
             show_modconfig(howner);             
           break; 
           
         case 333: //default
            set_defaults();
          break;    
         case 444: //browse
            
               
            
          break;                                                          
  	   }         
			break;
    case WM_SYSCOMMAND :
            if (wParam == SC_CLOSE){ 
              SendMessage(hwnd, WM_DESTROY, 0, 0);	
            return 1;  
           }     
      break;    
         
		case WM_CLOSE:
		      SendMessage(hwnd, WM_DESTROY, 0, 0);
		      return 1;
			break;
           		
		case WM_DESTROY:
		      EndDialog(hwnd, 0);
		      return 1;
			break;
	}
	return 0;
}



void show_configbox(HINSTANCE instance, HWND owner)
{
  m("Show config dlg");
  howner = owner;
  inst = instance;
  DialogBoxParamA(instance, MAKEINTRESOURCE(140), owner, (DLGPROC)cfg_dlg, 0);  
}  










